﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using Microsoft.DirectX;

namespace NGE
{
    class MenuBattleAct : Panelbox
    {
        public MenuBattleAct(Game g)
            : base(g)
        {
            gui_move = new Buttonbox();
            gui_move.Text = "移动";
            gui_move.Location = new Vector3(0, 0, 0);
            gui_move.OnButtonboxEvent += new GUIEvent(gui_move_OnButtonboxEvent);

            gui_attack = new Buttonbox();
            gui_attack.Text = "攻击";
            gui_attack.Location = new Vector3(0, 16, 0);
            gui_attack.OnButtonboxEvent += new GUIEvent(gui_attack_OnButtonboxEvent);

            gui_item = new Buttonbox();
            gui_item.Text = "道具";
            gui_item.Location = new Vector3(0, 32, 0);
            gui_item.OnButtonboxEvent += new GUIEvent(gui_item_OnButtonboxEvent);

            gui_drive = new Buttonbox();
            gui_drive.Text = "乘降";
            gui_drive.Location = new Vector3(0, 48, 0);
            gui_drive.OnButtonboxEvent += new GUIEvent(gui_drive_OnButtonboxEvent);

            gui_done = new Buttonbox();
            gui_done.Text = "完成";
            gui_done.Location = new Vector3(0, 64, 0);
            gui_done.OnButtonboxEvent += new GUIEvent(gui_done_OnButtonboxEvent);

            Buttonboxes.Add(gui_move);
            Buttonboxes.Add(gui_attack);
            Buttonboxes.Add(gui_item);
            Buttonboxes.Add(gui_drive);
            Buttonboxes.Add(gui_done);

            Location = new Vector3(0, 128, 0);
            Size = new Size(64, 96);

            OnPanelboxOpen += new GUIEvent(MenuBattleAct_OnPanelboxOpen);
            OnPanelboxClose += new GUIEvent(MenuBattleAct_OnPanelboxClose);
        }

        void MenuBattleAct_OnPanelboxClose(object sender, object arg)
        {
            //game.PC.IsWaitingCommand = false;
            //game.PC.LastActTime = game.RunningTotalSeconds;
        }

        void MenuBattleAct_OnPanelboxOpen(object sender, object arg)
        {
            //game.PC.IsWaitingCommand = true;
        }

        Buttonbox gui_move;
        Buttonbox gui_attack;
        Buttonbox gui_item;
        Buttonbox gui_drive;
        Buttonbox gui_done;

        protected override void OnKeyboardEventCode(object sender, int key, KeyboardState ks)
        {
            if (ks == KeyboardState.KeyDown)
            {
                if (game.PC.BattleAct == PlayerCharacterActs.BattleMoveCommanding || game.PC.BattleAct == PlayerCharacterActs.BattleAttackCommanding)
                {
                    if (key == game.Option.KeyU || key == game.Option.KeyL || key == game.Option.KeyD || key == game.Option.KeyR)
                    {
                        Direction dir = Direction.U;
                        int steps = 1;
                        if (key == game.Option.KeyU)
                        {
                            dir = Direction.U;
                            game.PC.TargetLocation.Y -= game.SCN.TileSize.Height;
                        }
                        else if (key == game.Option.KeyL)
                        {
                            dir = Direction.L;
                            game.PC.TargetLocation.X -= game.SCN.TileSize.Width;
                        }
                        else if (key == game.Option.KeyD)
                        {
                            dir = Direction.D;
                            game.PC.TargetLocation.Y += game.SCN.TileSize.Height;
                        }
                        else if (key == game.Option.KeyR)
                        {
                            dir = Direction.R;
                            game.PC.TargetLocation.X += game.SCN.TileSize.Width;
                        }

                        if (game.IsOnline)
                        {
                            game.ClientCOM.UploadPCBattleInfo(game.PC.BattleInfo);
                        }

                        if (game.PC.BattleAct == PlayerCharacterActs.BattleMoveCommanding)
                        {
                            Datagram.PlayerCharacterMoveInfo pcmi;
                            pcmi.Direction = dir;
                            pcmi.Steps = steps;
                            pcmi.Name = game.PC.Name;
                            pcmi.MoveSpeed = game.PC.MoveSpeed;

                            moveInfoHistory.Add(pcmi);
                        }
                    }
                    else if (key == game.Option.KeyA)
                    {
                        if (game.PC.BattleAct == PlayerCharacterActs.BattleMoveCommanding)
                        {
                            Vector3 v3 = game.PC.TargetLocation;
                            v3 = Util.Vector3SubVector3(v3, game.PC.Location);
                            v3.X /= game.SCN.TileSize.Width;
                            v3.Y /= game.SCN.TileSize.Height;
                            if (game.PC.CanMove(v3))
                            {
                                gui_attack.IsHide = gui_done.IsHide = gui_drive.IsHide = gui_item.IsHide = gui_move.IsHide = false;
                                game.PC.BattleAct = -1;
                                game.PC.TargetLocation = game.PC.Location;
                                game.ClientCOM.UploadPCBattleInfo(game.PC.BattleInfo);
                                genPath();
                            }
                            else
                            {
                                game.PC.TargetLocation = game.PC.Location;
                                moveInfoHistory.Clear();
                            }
                        }
                        else if (game.PC.BattleAct == PlayerCharacterActs.BattleAttackCommanding)
                        {
                            Vector3 v3 = game.PC.TargetLocation;
                            v3 = Util.Vector3SubVector3(v3, game.PC.Location);
                            v3.X /= game.SCN.TileSize.Width;
                            v3.Y /= game.SCN.TileSize.Height;
                            if (game.PC.CanAttack(v3))
                            {
                                gui_attack.IsHide = gui_done.IsHide = gui_drive.IsHide = gui_item.IsHide = gui_move.IsHide = false;
                                game.PC.BattleAct = -1;
                                game.PC.TargetLocation = game.PC.Location;
                                game.ClientCOM.UploadPCBattleInfo(game.PC.BattleInfo);
                            }
                            else
                            {
                                game.PC.TargetLocation = game.PC.Location;
                            }
                        }
                    }
                }
                else
                {
                    if (key == game.Option.KeyD)
                    {
                        FocusNextButtonbox();
                    }
                    else if (key == game.Option.KeyU)
                    {
                        FocusPreButtonbox();
                    }
                    else if (key == game.Option.KeyA)
                    {
                        Buttonboxes[focusedButtonboxIndex].Down(focusedButtonboxIndex);
                    }
                }
            }
        }

        void gui_done_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Focus)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = ">完成";
            }
            else if (bs == ButtonboxState.Up)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = "完成";
            }
            else if (bs == ButtonboxState.Down)
            {
                Close(this, null);
                //game.SceneCOM.NextOne();
            }
        }

        void gui_drive_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Focus)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = ">乘降";
            }
            else if (bs == ButtonboxState.Up)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = "乘降";
            }
        }

        void gui_item_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Focus)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = ">道具";
            }
            else if (bs == ButtonboxState.Up)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = "道具";
            }
        }

        void gui_attack_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Focus)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = ">攻击";
            }
            else if (bs == ButtonboxState.Up)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = "攻击";
            }
            else if (bs == ButtonboxState.Down)
            {
                gui_attack.IsHide = gui_done.IsHide = gui_drive.IsHide = gui_item.IsHide = gui_move.IsHide = true;
                game.PC.TargetLocation = game.PC.Location;
                game.PC.BattleAct = PlayerCharacterActs.BattleAttackCommanding;
            }
        }

        List<Datagram.PlayerCharacterMoveInfo> moveInfoHistory;

        void genPath()
        {
            //List<Datagram.PlayerCharacterMoveInfo> bak = new List<Datagram.PlayerCharacterMoveInfo>();
            //for (int i = 1; i < moveInfoHistory.Count; i++)
            //{
            //    if (moveInfoHistory[i - 1].Direction != Util.GetOppositeDirection(moveInfoHistory[i].Direction))
            //    {
            //        bak.Add(moveInfoHistory[i - 1]);
            //        if (i + 1 == moveInfoHistory.Count)
            //        {
            //            bak.Add(moveInfoHistory[i]);
            //        }
            //    }
            //}
            //moveInfoHistory = new List<Datagram.PlayerCharacterMoveInfo>();
            foreach (Datagram.PlayerCharacterMoveInfo pcmi in moveInfoHistory)
            {
                game.PC.Move(pcmi);
            }
        }

        void gui_move_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Focus)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = ">移动";
            }
            else if (bs == ButtonboxState.Up)
            {
                Buttonbox bb = (Buttonbox)sender;
                bb.Text = "移动";
            }
            else if (bs == ButtonboxState.Down)
            {
                gui_attack.IsHide = gui_done.IsHide = gui_drive.IsHide = gui_item.IsHide = gui_move.IsHide = true;
                moveInfoHistory = new List<Datagram.PlayerCharacterMoveInfo>();

                game.PC.BattleAct = PlayerCharacterActs.BattleMoveCommanding;
                game.PC.TargetLocation = game.PC.Location;
                game.ClientCOM.UploadPCBattleInfo(game.PC.BattleInfo);
            }
        }
    }
}
